package com.d3works.apps.production.service;

import com.d3works.apps.base.db.repo.PartnerRepository;
import com.d3works.apps.base.service.administration.SequenceService;
import com.d3works.apps.base.service.user.UserService;
import com.d3works.apps.production.service.app.AppProductionService;
import com.d3works.apps.production.service.productionorder.ProductionOrderSaleOrderService;
import com.d3works.apps.sale.db.SaleOrder;
import com.d3works.apps.sale.db.repo.SaleOrderRepository;
import com.d3works.apps.sale.service.app.AppSaleService;
import com.d3works.apps.sale.service.config.SaleConfigService;
import com.d3works.apps.sale.service.saleorder.SaleOrderLineService;
import com.d3works.apps.supplychain.service.AccountingSituationSupplychainService;
import com.d3works.apps.supplychain.service.PartnerSupplychainService;
import com.d3works.apps.supplychain.service.SaleOrderCheckAnalyticService;
import com.d3works.apps.supplychain.service.SaleOrderPurchaseService;
import com.d3works.apps.supplychain.service.SaleOrderStockService;
import com.d3works.apps.supplychain.service.SaleOrderWorkflowServiceSupplychainImpl;
import com.d3works.apps.supplychain.service.app.AppSupplychainService;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class SaleOrderWorkflowServiceProductionImpl
    extends SaleOrderWorkflowServiceSupplychainImpl {

  protected ProductionOrderSaleOrderService productionOrderSaleOrderService;
  protected AppProductionService appProductionService;

  @Inject
  public SaleOrderWorkflowServiceProductionImpl(
      SequenceService sequenceService,
      PartnerRepository partnerRepo,
      SaleOrderRepository saleOrderRepo,
      AppSaleService appSaleService,
      UserService userService,
      SaleOrderLineService saleOrderLineService,
      SaleOrderStockService saleOrderStockService,
      SaleOrderPurchaseService saleOrderPurchaseService,
      AppSupplychainService appSupplychainService,
      AccountingSituationSupplychainService accountingSituationSupplychainService,
      ProductionOrderSaleOrderService productionOrderSaleOrderService,
      AppProductionService appProductionService,
      PartnerSupplychainService partnerSupplychainService,
      SaleConfigService saleConfigService,
      SaleOrderCheckAnalyticService saleOrderCheckAnalyticService) {

    super(
        sequenceService,
        partnerRepo,
        saleOrderRepo,
        appSaleService,
        userService,
        saleOrderLineService,
        saleOrderStockService,
        saleOrderPurchaseService,
        appSupplychainService,
        accountingSituationSupplychainService,
        partnerSupplychainService,
        saleConfigService,
        saleOrderCheckAnalyticService);

    this.productionOrderSaleOrderService = productionOrderSaleOrderService;
    this.appProductionService = appProductionService;
  }

  @Override
  @Transactional(rollbackOn = {Exception.class})
  public void confirmSaleOrder(SaleOrder saleOrder) throws D3worksException {
    super.confirmSaleOrder(saleOrder);

    if (appProductionService.isApp("production")
        && appProductionService.getAppProduction().getProductionOrderGenerationAuto()) {
      productionOrderSaleOrderService.generateProductionOrder(saleOrder);
    }
  }
}
